package com.tuanzi.loan.business.service.statistics.impl;

import com.tuanzi.loan.business.entity.family.ProductInfo;
import com.tuanzi.loan.business.repository.statistics.NewOneKeyStatRepository;
import com.tuanzi.loan.business.service.statistics.NewOneKeyStatService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.statistics.newonekey.NewOneKeyStatRequest;
import com.tuanzi.loan.vo.response.statistics.newonekey.NewOneKeyStatDetailResponse;
import com.tuanzi.loan.vo.response.statistics.newonekey.NewOneKeyStatTotalResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author caiwanlin
 * @create 2018-06-27 15:41
 */
@Debug
@Service
@Transactional
@Retryable(value = {RetryException.class})
public class NewOneKeyStatServiceImpl implements NewOneKeyStatService {

    @Autowired
    private NewOneKeyStatRepository newOneKeyStatRepository;

    @Override
    public Page<NewOneKeyStatTotalResponse> totalList(PageSearcher<NewOneKeyStatRequest> pageSearcher) {
        return newOneKeyStatRepository.totalList(pageSearcher.getSearchCondition(),pageSearcher.getPageable());
    }

    @Override
    public Page<NewOneKeyStatDetailResponse> detailList(PageSearcher<NewOneKeyStatRequest> pageSearcher) {
        Page<NewOneKeyStatDetailResponse> responses = newOneKeyStatRepository.detailList(pageSearcher.getSearchCondition(),
                pageSearcher.getPageable());
        List<NewOneKeyStatDetailResponse> content = responses.getContent();
        if (content == null || content.isEmpty()) return responses;
        NewOneKeyStatRequest request = pageSearcher.getSearchCondition();
        Integer productId = request.getProductId();
        Integer channel = request.getChannel();
        String platform = request.getPlatform();
        content.forEach(con->{
            if (productId == null) con.setProductName("全部");
            if (channel == null) con.setChannelName("全部");
            if (StringUtils.isBlank(platform)) con.setPlatform("全部");
            Integer partyType = con.getPartyType();
            if (partyType == 1){
                con.setPartyName("H5");
            }
            else if (partyType == 2){
                con.setPartyName("联登");
            }
            else if (partyType == 3){
                con.setPartyName("API");
            }
            else if (partyType == 4){
                con.setProductName("全部");
                con.setChannelName("全部");
                con.setPlatform("全部");
                con.setPartyName("借条");
            }
        });
        return responses;
    }

    @Override
    public List<ProductInfo> getProductBySpaceId(Integer spaceId) throws Exception {
        return newOneKeyStatRepository.getProductBySpaceId(spaceId);
    }
}
